<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="display"
      :before-close="handleClose"
    >
      <import-repo
        v-if="type == 'upload'"
        @click="handleClose"
        :dialogType="dialogType"
        :search_data="search_data"
      ></import-repo>
      <delete
        v-if="type == 'delete'"
        :content="delete_content"
        :handleCancel="handleClose"
        :handleOk="handleDeleteOk"
      >
      </delete>
    </el-dialog>

    <ky-table ref="table" :getData="getRpmList" :showSelect="showSelect">
      <template v-slot:table-action>
        <authorization-button
          class="kylin-item-button"
          :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
          @click="handleDelete"
          name="Repository:deleteRpm"
          >{{ $t("patch.delete_rpm") }}</authorization-button
        >
        <authorization-button
          class="kylin-item-button"
          @click="handleRepoUpload"
          name="Repository:uploadRpm"
          >{{ $t("patch.upload_packages") }}</authorization-button
        >
        <authorization-button
          class="kylin-item-button"
          @click="handleCVEUpload"
          >{{ $t("patch.upload_CVE") }}</authorization-button
        >
      </template>
      <template v-slot:table>
        <el-table-column prop="nvra" :label="$t('name')"> </el-table-column>
      </template>
    </ky-table>
  </div>
</template>

<script>
import { getRpmList, deleteRpm } from "@/api/patch";
import kyTable from "@/components/KyTable";
import importRepo from "../form/importRepo.vue";
import AuthorizationButton from "@/components/AuthorizationButton";

import Delete from "@/components/Delete";
export default {
  components: {
    kyTable,
    importRepo,
    AuthorizationButton,
    Delete,
  },
  props: {
    form: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      dialogType: "",
      title: "",
      display: false,
      showSelect: true,
      search_data: {
        name: {
          name: "name",
          type: "text",
        },
      },
      display: false,
      delete_content: "",
      type: "",
    };
  },
  methods: {
    getRpmList(data) {
      return getRpmList(data, this.$route.query.repoId);
    },
    handleDelete() {
      this.display = true;
      this.type = "delete";
      this.title = this.$t("patch.delete_rpm");
      let arr = [];
      this.$refs.table.selectRow.rows.map(function (row) {
        arr.push(row.name);
      });
      this.delete_content = this.$t("patch.delete_rpm_content").replace(
        "%s",
        arr.toString()
      );
    },
    async handleDeleteOk() {
      let data = {};
      if (this.$refs.table.select_id === "2") {
        const res = await this.getRpmList({
          ...this.search_criteria,
          ...this.$route.query,
          paged: false,
        });
        let ids = [];
        res.data.data.results.map((item) => {
          if (!this.$refs.table.selectRow.excludeIds.includes(item.id)) {
              ids.push(item.id);
          }
        });
        data["rpmIds"] = ids;
      } else {
        data["rpmIds"] = this.$refs.table.selectRow.ids;
      }
      const res = await deleteRpm({
        repoId: this.$route.query.repoId,
        id: this.$route.params.id,
        data,
      });
      if (res.data.code == "0") {
        this.$message({
          type: "success",
          message: res.data.message,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.message,
        });
      }
      this.display = false;
      this.edit_id = null;
      this.type = "";
      this.$refs.table.handleSearch();
    },
    handleRepoUpload() {
      this.dialogType = "repo";
      this.display = true;
      this.type = "upload";
    },
    handleCVEUpload() {
      this.dialogType = "CVE";
      this.display = true;
      this.type = "upload";
    },
    handleClose() {
      this.display = false;
      this.$refs.table.handleSearch();
      this.display = false;
      this.type = "";
      this.title = "";
    },
  },
  watch: {
    form: {
      handler(newvalue, oldvalue) {
        if (!this.$store.state.patch.selectRepo) {
          this.$store.commit("set_repo", { selectRepo: newvalue.name });
        }
      },
      deep: true,
    },
  },
};
</script>

<style>
</style>